import logging
import threading

from django.core.mail import EmailMultiAlternatives
from django.conf import settings

from brispon import brispon_setting as bs

"""
http://www.artfulcode.net/articles/threading-django/
"""
def send_email(to, subject, contents):
    subject = subject
    from_email = getattr(bs, 'FROM_EMAIL', 'noreply@gluee.net') 
    to_email = to
    text_content = contents
    html_content = contents
    try:
        msg = EmailMultiAlternatives(subject, text_content, from_email, [to_email,])
        msg.attach_alternative(html_content, "text/html")
        msg.send()
        
        # Create a new thread in Daemon mode to send message
        t = threading.Thread(target=msg.send)
        t.setDaemon(True)
        t.start()
        
    except:
        logging.error("Can not send email")